﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using MsDAL;
using System.Data;
using System.Collections;
using Components;

namespace ServiceHelper
{
    public class CommonManager
    {
        private CommonDAL dal = new CommonDAL();

        private static CommonManager _entity;
        /// <summary>
        /// 得到CommonManager类的实体对象
        /// </summary>
        public static CommonManager Entity
        {
            get
            {
                if (_entity == null)
                    _entity = new CommonManager();
                return _entity;
            }
        }

        /// <summary>
        /// Main页面默认分页时，取得总记录数
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public int GetDefaultRecordCount(Pager pager)
        {
            pager.For = PagerFor.Count;
            return dal.GetPageRecoredCount(pager);
        }

        /// <summary>
        /// 得到页面数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public DataTable GetPageData(Pager page)
        {
            page.For = PagerFor.Data; 
            return dal.GetPageData(page);
        }

        /// <summary>
        /// 得到页面数据
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public DataTable GetSummery(Pager page)
        {
            page.For = PagerFor.Summary;
            return dal.GetPageData(page);
        }

        /// <summary>
        /// 加载数据库内设置的数据源
        /// </summary>
        /// <param name="page"></param>
        /// <param name="action"></param>
        /// <param name="ctl"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable GetSys5a02Data(string page, string action, string ctl, string where)
        {
            return dal.GetSys5a02Data(page, action, ctl, where);
        }

        /// <summary>
        /// 检测数据库中是否存在值
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public bool HasData(string tb, string where)
        {
            Hashtable hs = StringUtil.Entity.JsonToTable(where, false);
            return HasData(tb, hs);
        }

        /// <summary>
        /// 检测数据库中是否存在值
        /// </summary>
        /// <param name="tb"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public bool HasData(string tb, Hashtable where)
        {
            return dal.HasData(tb, where);
        }

        /// <summary>
        /// 批量执行SQLAPP对象
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public string SaveData(List<SqlApp> list)
        {
            return (null == list || 0 == list.Count) ? "没有数据被保存！" : DBHelper.BatchDoSql(list);
        }

        public string SaveData(params SqlApp[] array)
        {
            var list = new List<SqlApp>();

            list.AddRange(array);

            return SaveData(list);
        }

        /// <summary>
        /// 执行数据库的自定义函数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable ExecDBFun(string key, string where)
        {
            Hashtable hs = StringUtil.Entity.JsonToTable(where, false);
            return ExecDBFun(key, hs);
        }

        /// <summary>
        /// 执行数据库的自定义函数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable ExecDBFun(string key, Hashtable where)
        {
            return dal.ExecDbFun(key, where);
        }


        public object ExecDbFunScalar(string key, string where)
        {
            Hashtable hs = StringUtil.Entity.JsonToTable(where, false);
            return dal.ExecDbFunScalar(key, hs);
        }

        public object ExecDbFunScalar(string key, Hashtable where)
        {
            return dal.ExecDbFunScalar(key, where);
        }

    }
}
